<?php

namespace app\admin\controller\jdy;

use app\common\controller\Backend;
use think\Session;
/**
 * 案場修改记录管理
 *
 * @icon fa fa-circle-o
 */
class Anchanglog extends Backend
{
    
    /**
     * Anchanglog模型对象
     * @var \app\admin\model\jdy\Anchanglog
     */
    protected $model = null;

    const ANCHANG_ID = "__aloganchangid__";

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\jdy\Anchanglog;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        $anchang_id = 0;
        $url = $_SERVER['HTTP_REFERER'];
        if (false !== strpos($url, '/ids/')) {
            $urls = explode('?', $url);
            $url = $urls[0];
            $urls = explode('/', $url);
            $size = count($urls);
            $anchang_id = $urls[$size - 1];
            Session::set(self::ANCHANG_ID, $anchang_id);
            $anchang = \app\admin\model\jdy\Anchang::get($anchang_id);
            if ($anchang) {
                $anchangname = $anchang->name;
            }
        } else {
            Session::set(self::ANCHANG_ID, 0);
        }

        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            if ($anchang_id > 0) {
                $total = $this->model
                    ->with(['anchang'])
                    ->where($where)
                    ->where('anchang_id', $anchang_id)
                    ->order($sort, $order)
                    ->count();

                $list = $this->model
                    ->with(['anchang'])
                    ->where($where)
                    ->where('anchang_id', $anchang_id)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            } else {
                $total = $this->model
                    ->with(['anchang'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

                $list = $this->model
                    ->with(['anchang'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            }

            $list = collection($list)->toArray();
            foreach ($list as &$row) {
                $prearray = json_decode($row['pre'], TRUE);
                $nextarray = json_decode($row['next'], TRUE);

                $change = "";
                foreach ($nextarray as $k => $v) {
                    $v0 = $prearray[$k];
                    if ($v0 != $v) {
                        $change .= "[" . $k . "]" . $v0 . "=>" . $v . "; ";
                    }
                }
                $row['change'] = $change;
            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        $this->view->assign("anchangname", $anchangname);
        return $this->view->fetch();
    }
}
